package com.sgb.knowledgeshare.view.fragment;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.FragmentShareVideoplayUserBinding;
import com.sgb.knowledgeshare.entity.ShareUserInfoEntity;
import com.sgb.knowledgeshare.utils.RxBus;
import com.sgb.knowledgeshare.utils.VideoPlayListUser;
import com.sgb.knowledgeshare.view.activity.ShareUserAttentionActivity;
import com.sgb.knowledgeshare.view.activity.ShareUserFansActivity;
import com.swgk.core.view.adapter.CommPagerAdapter;
import com.sgb.knowledgeshare.viewmodel.ShareVideoPlayUserModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import java.util.ArrayList;
import rx.functions.Action1;

/**
 * Created by fq on 2020/9/11 0011
 * Description：分享模块点击单个视频向左滑动展示用户信息通用类
 */
public class ShareVideoPlayUserFragment extends AppLazyFragment {

    private static String USER_ID = "userId";
    ShareVideoPlayUserModel viewmodel;
    private FragmentShareVideoplayUserBinding binding;
    private ArrayList<Fragment> mFragments;
    private String userid;
    private String  uservideoid = "0";

    public static ShareVideoPlayUserFragment newInstance(String userId) {
        ShareVideoPlayUserFragment fragment = new ShareVideoPlayUserFragment();
        Bundle bundle = new Bundle();
        bundle.putString(USER_ID, userId);
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_share_videoplay_user, container, false);
        viewmodel = new ViewModelProvider(this).get(ShareVideoPlayUserModel.class);
        viewmodel.setActivity(ShareVideoPlayUserFragment.this);
        if (getArguments() != null) {
            userid = getArguments().getString(USER_ID);
        }
        return binding.getRoot();
    }

    @Override
    protected void onLazyLoad() {
        RxBus.getDefault().toObservable(VideoPlayListUser.class).subscribe((Action1<VideoPlayListUser>) VideoPlayListUser -> {
            userid = VideoPlayListUser.getUserid();
            // 获取用户信息数据
            if (!TextUtils.isEmpty(userid)){
                if (!uservideoid.equals(userid)){
                    uservideoid = userid;
                    initData(uservideoid);
                }
            }
        });
        // 获取用户信息数据
        if (!TextUtils.isEmpty(userid)){
            if (!uservideoid.equals(userid)){
                uservideoid = userid;
                initData(uservideoid);
            }
        }
    }

    @Override
    public void onVisible() {
        super.onVisible();
        // 获取用户信息数据
        if (!TextUtils.isEmpty(userid)){
            if (!uservideoid.equals(userid)){
                uservideoid = userid;
                initData(uservideoid);
            }
        }

    }

    private void initData(String userid) {
        viewmodel.getUserHomePage(userid);
    }

    @Override
    protected void initView() {
        binding.llShareUserLike.setOnClickListener(this);//获赞
        binding.llShareUserAttention.setOnClickListener(this);//关注
        binding.llShareUserFans.setOnClickListener(this);//粉丝
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();//点赞
        if (id == R.id.ll_share_user_like) {
        } else if (id == R.id.ll_share_user_attention) {//关注
            ShareUserAttentionActivity.start(getContext(), uservideoid);
        } else if (id == R.id.ll_share_user_fans) {//粉丝
            ShareUserFansActivity.start(getContext(), uservideoid);
        }
    }

    public void setUserData(ShareUserInfoEntity data) {
        if (!isAdded()) return;
//        头像
        if (!TextUtils.isEmpty(data.getPhoto())) {
            Glide.with(getContext()).load(data.getPhoto() + "")
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()).placeholder(R.mipmap.share_def_photo))
                    .into(binding.shareUserLogo);
        }
//        是否关注状态   0未关注、1已关注、2互相关注
//        share_user_state
        int isUserHomePage = data.getIsUserHomePage();
        if (isUserHomePage == 1) {//是否时自己的主页
            binding.shareUserState.setVisibility(View.GONE);
        } else {
            binding.shareUserState.setVisibility(View.VISIBLE);
        }
        switch (data.getIsFollow()) {
            case 0:
                binding.shareUserState.setText("关注");
                binding.shareUserState.setBackgroundColor(getResources().getColor(R.color.color_5792FD));
                break;
            case 1:
                binding.shareUserState.setText("取消关注");
                binding.shareUserState.setBackgroundColor(getResources().getColor(R.color.color_C0C4CC));
                break;
            case 2:
                binding.shareUserState.setText("互相关注");
                binding.shareUserState.setBackgroundColor(getResources().getColor(R.color.color_FFC000));
                break;
        }

//        用户名称
        if (!TextUtils.isEmpty(data.getName())) {
            binding.shareUserName.setText(data.getName());
        } else {
            binding.shareUserName.setText("");
        }
        //公司名称
        if (!TextUtils.isEmpty(data.getCompanyName())) {
            binding.shareUserCompany.setText(data.getCompanyName());
        } else {
            binding.shareUserCompany.setText("");
        }
        //获赞数量
        binding.shareUserLike.setText(data.getLaudCount() + "");
        //关注数量
        binding.shareUserAttention.setText(data.getFollow() + "");
        //粉丝数量
        binding.shareUserFans.setText(data.getFens() + "");
        initListData(data);
    }

    private void initListData(ShareUserInfoEntity data) {
        String[] titles = {"作品  " + data.getCreateCount(), "喜欢  " + data.getLikeCount()};
    /*    mFragments = new ArrayList<>();
        mFragments.add(VideoPlayUserListFragment.newInstance(data.getUserNo(), 0));
        mFragments.add(VideoPlayUserListFragment.newInstance(data.getUserNo(), 1));*/
        CommPagerAdapter mAdapetr = new CommPagerAdapter(getChildFragmentManager(), mFragments, titles);
        binding.shareuserViewpagermap.setAdapter(mAdapetr);
        binding.shareuserTablayout.setViewPager(binding.shareuserViewpagermap);
//        关注的点击事件
        binding.shareUserState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    if (SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH, false)) {
                        //请求接口数据、成功后再获取数据
                        viewmodel.getFens(data.getUserNo());
                    } else {
                        MToast.showToast(getContext(), "请实名认证完成后再进行点赞！");
                    }
                } else {
                    MToast.showToast(getContext(), "请先登录账户！");
                }
            }
        });
    }
    public void UpdataUserIfno() {
        viewmodel.getUserHomePage(userid);
    }
}
